package com.loveprogrammer.eternity.factory;

import com.alibaba.fastjson.JSONArray;
import com.loveprogrammer.eternity.action.BaseAction;
import com.loveprogrammer.eternity.enums.QualityEnum;
import com.loveprogrammer.eternity.factory.template.EffectTemplate;
import com.loveprogrammer.eternity.factory.template.ThingsTemplate;
import com.loveprogrammer.eternity.model.product.Things;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName ThingsFactory
 * @Description 物品工厂
 * @Author admin
 * @Date 2023/9/11 9:39
 * @Version 1.0
 */
public class ThingsFactory  {

    private static Map<Integer, ThingsTemplate> map;

    static {
        try {
            // 读取配置文件，然后加载到weaponTemplates中
            ClassLoader classLoader = EquipmentFactory.class.getClassLoader();
            InputStream inputStream = classLoader.getResourceAsStream("things.json");

            assert inputStream != null;
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
            StringBuilder responseStrBuilder = new StringBuilder();

            String inputStr;
            while ((inputStr = streamReader.readLine()) != null)
                responseStrBuilder.append(inputStr);

            List<ThingsTemplate> list = JSONArray.parseArray(responseStrBuilder.toString(), ThingsTemplate.class);

            map = list.stream().collect(Collectors.toMap(ThingsTemplate::getId, e->e));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Things getThingsById(int id, QualityEnum qualityEnum) {
        ThingsTemplate thingsTemplate = map.get(id);
        return new Things(thingsTemplate);
    }
}
